#include "SQLiteSingleton.h"

    // 初始化静态成员变量
SQLite3Singleton* SQLite3Singleton::instance = nullptr;
SQLite3Singleton::SQLite3Singleton(const string& dbName) {
    this->dbName = dbName;
}

SQLite3Singleton::~SQLite3Singleton() {
    
}

SQLite3Singleton* SQLite3Singleton::getInstance(const std::string& dbName) {
    if (instance == nullptr) {
        instance = new SQLite3Singleton(dbName);
    }
    return instance;
}

ReturnStatus SQLite3Singleton::executeQuery(const std::string& query) {
    char* errMsg;
    int rc = sqlite3_exec(db, query.c_str(), 0, 0, &errMsg);
    if (rc != SQLITE_OK) {
        throw runtime_error(errMsg);
        cerr << "操作失败 " << errMsg << std::endl;
        sqlite3_free(errMsg);
        return FAILURE;
    }
    cout << "操作成功" << std::endl;

    return SUCCESS;
}
ReturnStatus SQLite3Singleton::connect(){
    ReturnStatus status;
    int rc = sqlite3_open(dbName.c_str(), &db);
    if (rc) {
        //cerr << "数据库链接失败" << sqlite3_errmsg(db) << endl;
        // 连接失败，进行相应处理
        throw runtime_error("数据库链接失败");
        status = FAILURE;
    } else {
        cout << "数据库链接成功" << endl;
        // 连接成功，继续后续操作
        status = SUCCESS;
    }
    return status;
}
ReturnStatus SQLite3Singleton::disconnect(){
    ReturnStatus status;
    int rc =sqlite3_close(db);
    if (rc == SQLITE_OK) {
        cout << "数据库关闭成功" << std::endl;
        
        status = SUCCESS;
    } else {
        throw runtime_error("数据库关闭失败");
        //cerr << "数据库关闭失败 " << sqlite3_errmsg(db) << std::endl;
        status = FAILURE;
    }
    return status;
}

vector<string> SQLite3Singleton::fetchResults(const string& query,const TableChoice& tableChoice){

    
    vector<string> results;
    sqlite3_stmt *stmt;
    int rc = sqlite3_prepare_v2(db, query.c_str(), -1, &stmt, nullptr);
    if (rc != SQLITE_OK) {
        throw runtime_error("SQL查询失败");
        //cerr << "SQL查询失败 " << sqlite3_errmsg(db) << std::endl;
        return results;
    }
    // 执行查询
    while ((rc = sqlite3_step(stmt))==SQLITE_ROW)
    {
        /* code */

        int id = sqlite3_column_int(stmt, 0);
        if (tableChoice == TABLEDEVICE) {
            int device_type = sqlite3_column_int(stmt, 1);
            string status = (char*)sqlite3_column_text(stmt, 2);
            string last_modified = (char*)sqlite3_column_text(stmt, 3);
            results.push_back(to_string(id));
            results.emplace_back(to_string(device_type));
            results.emplace_back(status);
            results.push_back(last_modified);
        } else if (tableChoice == TABLELOG) {
            string log_type = (char*)sqlite3_column_text(stmt, 1);
            string timestamp = (char*)sqlite3_column_text(stmt, 2);
            int user_id = sqlite3_column_int(stmt, 3);
            int device_id = sqlite3_column_int(stmt, 4);
            string log_content = (char*)sqlite3_column_text(stmt, 2);
            results.push_back(to_string(id));
            results.emplace_back(log_type);
            results.emplace_back(timestamp);
            results.push_back(to_string(user_id));
            results.push_back(to_string(device_id));
            results.push_back(log_content);
        } else if (tableChoice == TABLEUSER) {

            string username = (char*)sqlite3_column_text(stmt, 1);
            string password = (char*)sqlite3_column_text(stmt, 2);
            Role role = (Role)sqlite3_column_int(stmt, 3);
            results.push_back(to_string(id));
            results.emplace_back(username);
            results.emplace_back(password);
            results.push_back(to_string(role));
        
        }
        
    }
    return results;
    
    
}